package com.drinks.yiniang.ui.activity.user.wine;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.drinks.yiniang.entity.common.StringJson;
import com.drinks.yiniang.entity.goods.GoodsObject;
import com.drinks.yiniang.io.api.WineListApiIO;
import com.drinks.yiniang.utils.GlideUtils;
import com.drinks.yiniang.utils.ToastUtils;
import com.hansen.library.io.tuple.Tuple2;
import com.hansen.library.listener.APIRequestCallback;
import com.hansen.library.ui.widget.image.RatioRoundImageView;
import com.hansen.library.Constants;
import com.hansen.library.listener.OnNavgationBarClickListener;
import com.hansen.library.ui.activity.BaseTranBarActivity;
import com.hansen.library.ui.widget.nav.NavigationBarLayout;
import com.hansen.library.utils.StringUtils;

/**
 * 添加到酒单页面
 *
 * @author han
 * @date 2019/09/03
 */
public class AddToWineListActivity extends BaseTranBarActivity implements OnNavgationBarClickListener {

    //nav
    private NavigationBarLayout nav_bar_add_to_wine_list;
    //酒单名称
    private TextView tv_add_to_wine_list_name;
    //商品封面
    private RatioRoundImageView iv_wine_list_goods_cover;
    //商品名称
    private TextView tv_wine_list_goods_name;
    //商品推荐语
    private TextView tv_wine_list_goods_slogan;
    //价格
    private TextView tv_wine_list_goods_price;
    //加入购物车
    private ImageView iv_wine_list_goods_add_cart;
    //描述
    private EditText et_add_to_wine_list_describe;
    //完成
    private Button btn_add_to_wine_list_done;

    //商品object
    private GoodsObject mGoodsObject;
    //酒单id
    private String mWineListId;
    //酒单标题
    private String mWineListTitle;

    @Override
    protected void initView() {
        nav_bar_add_to_wine_list = findViewById(R.id.nav_bar_add_to_wine_list);

        tv_add_to_wine_list_name = findViewById(R.id.tv_add_to_wine_list_name);
        tv_wine_list_goods_name = findViewById(R.id.tv_wine_list_goods_name);
        tv_wine_list_goods_slogan = findViewById(R.id.tv_wine_list_goods_slogan);
        tv_wine_list_goods_price = findViewById(R.id.tv_wine_list_goods_price);

        iv_wine_list_goods_cover = findViewById(R.id.iv_wine_list_goods_cover);
        iv_wine_list_goods_add_cart = findViewById(R.id.iv_wine_list_goods_add_cart);

        et_add_to_wine_list_describe = findViewById(R.id.et_add_to_wine_list_describe);

        btn_add_to_wine_list_done = findViewById(R.id.btn_add_to_wine_list_done);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mContext = this;

        if(getIntent() != null) {
            mGoodsObject = (GoodsObject) getIntent().getSerializableExtra(Constants.KeyObject);
            mWineListId = getIntent().getStringExtra(Constants.KeyWineListId);
            mWineListTitle = getIntent().getStringExtra(Constants.KeyTitle);
        }

        setGoodsData();
    }

    @Override
    protected void initListener() {
        nav_bar_add_to_wine_list.setOnNavgationBarClickListener(this);
        btn_add_to_wine_list_done.setOnClickListener(this);
    }

    @Override
    public void widgetClick(View v) {
        if(v.getId() == R.id.btn_add_to_wine_list_done) {
            joinWineList();
        }
    }

    @Override
    public int getTranBarResID() {
        return R.layout.activity_add_to_wine_list;
    }

    @Override
    public void onBackClick(View v) {
        finish();
    }

    @Override
    public void onEditClick(View v) {

    }

    //加入酒单
    private void joinWineList() {
        if(mGoodsObject == null || StringUtils.isEmpty(mGoodsObject.getId())) {
            ToastUtils.showShort(getString(R.string.text_the_goods_does_not_exist));
            return;
        }

        if(StringUtils.isEmpty(mWineListId)) {
            ToastUtils.showShort(getString(R.string.text_wine_list_does_not_exist));
            return;
        }

        String desc = StringUtils.getEditTextString(et_add_to_wine_list_describe.getText());

        showLoadingDialog();

        WineListApiIO.getInstance().joinWineList(mWineListId, mGoodsObject.getId(), desc, new APIRequestCallback<StringJson, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(StringJson res) {
                ToastUtils.showShort(getString(R.string.text_join_success));
                setResult(Constants.RESULT_CODE_NORMAL);
                finish();
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                ToastUtils.showShort(error._2);
            }

            @Override
            public void onComplete() {
                dismissDialog();
            }
        });
    }

    private void setGoodsData() {
        tv_add_to_wine_list_name.setText(mWineListTitle);

        if(mGoodsObject == null) {
            return;
        }

        GlideUtils.loadSquareImage(mContext, iv_wine_list_goods_cover, mGoodsObject.getCoverUrl());
        tv_wine_list_goods_name.setText(mGoodsObject.getName());
        tv_wine_list_goods_slogan.setText(mGoodsObject.getSub_title());
        tv_wine_list_goods_price.setText("¥" + StringUtils.getNullEmptyConvert__(mGoodsObject.getApp_price()));
    }
}
